<?php
	include ('../defines.php');

	FunctionsHelper::includeFiles(
		array(
			ADMINISTRATOR_CONTROLLER_DIRECTORY . 'users.php',
			ADMINISTRATOR_CONTROLLER_DIRECTORY . 'tournaments.php'
		)
	);

	$users = new Users();
	if (!$users->isLoggedUser())
		header('Location:' . ADMINISTRATOR_VIEW_PATH . 'login.php');

	$userLogged = $users->getLoggedUser();

	$tournaments = new Tournaments();
	if (!empty($_POST)){	
		$action = $_POST['action'];
		unset($_POST['action']);
		unset($_POST['sbmSubmit']);

		if (!empty($_FILES['image']['name'])){
			$_POST['image'] = $_FILES['image'];
			$_POST['deleteImage'] = 1; 
		}

		if ($action == 'm')
			$operation = $tournaments->updateTournament($_POST);
		else 
			$operation = $tournaments->createTournament($_POST);

		if ($operation)
			header('Location:' . ADMINISTRATOR_VIEW_PATH . 'tournaments.php');
	}

	if (isset($_GET['id'])){
		$tournament = $tournaments->getTournament(
			array(
				'tournament_id' => $_GET['id']
			)
		);
	}else{
		$tournament = $tournaments->getTournament();
	}
	
	$tournamentCategories = $tournaments->getCategories();

	$class = $tournament->getClass();
	if (!empty($class))
		$SelectedCategory = $tournament->getClass()->getClassId();
	else
		$SelectedCategory = 0;
	
	$sex = $tournament->getSex();
	$manChecked = 'checked="checked"';
	$womanChecked = '';
	if (isset($sex)){
		if ($sex == 'F'){
			$manChecked = '';
			$womanChecked = 'checked="checked"';
		}
	}
	
	FunctionsHelper::includeTop();
?>
<div class="withBorder" id="main">
	<script type="text/javascript">
		$(function(){
			$('#back').click(function(){
				document.location.href='<?=ADMINISTRATOR_VIEW_PATH;?>tournaments.php';
			});
			$('#class_id').change(function(){
				addRequiredFields($(this));
			});
			addRequiredFields($('#class_id'));
		});

		var addRequiredFields = function(element){
			if (element.val() == 10){
				$.each($('.notAllwaysRequired'), function(){
					$(this).removeClass('required');
				});
			}else{
				$.each($('.notAllwaysRequired'), function(){
					$(this).addClass('required');
				});
			}
		};
	</script>
	<div class="mainABM bigMain">
		<div class="ABMtitle"><h1>ADMINISTRADOR DE TORNEOS</h1></div>
		<form id='frmTournament' class='form' method='POST' enctype="multipart/form-data">
			<input type='hidden' name='tournament_id' id='tournament_id' value='<?= $tournament->getTournamentId(); ?>' />
			<input type='hidden' name='action' id='action' value='<?= $_GET['action']; ?>' />
			<div class="label">Nombre: </div><input type='text' value='<?= $tournament->getName(); ?>' class='required bigInput' name='name' id='name' />
			<br />
			<div class="label">Sexo: </div>M<input type="radio" name="sex" class="sex" <?=$manChecked;?> value="M">F<input type="radio" name="sex" class="sex" <?=$womanChecked;?> value="F">
			<br />
			<div class="label">Lugar: </div><input type='text' value='<?= $tournament->getPlace(); ?>' class='required bigInput notAllwaysRequired' name='place' id='place' />
			<br />
			<div class="label">Superficie: </div><input type='text' value='<?= $tournament->getSurface(); ?>' class='required bigInput notAllwaysRequired' name='surface' id='surface' />
			<br />
			<div class="label">Fecha Inicial: </div><input type='text' value='<?= date('d/m/Y', strtotime($tournament->getInitialDate())); ?>' class='required tinyInput dateP' name='initial_date' id='initial_date' />
			<br />
			<div class="label">Fecha Final: </div><input type='text' value='<?= date('d/m/Y', strtotime($tournament->getFinalDate())); ?>' class='required tinyInput dateP' name='final_date' id='final_date' />
			<br />
			<div class="label">Puntos: </div><input type='text' value='<?= $tournament->getPoints(); ?>' class='required tinyInput notAllwaysRequired' name='points' id='points' />
			<br />
			<div class="label">Descripci&oacute;n: </div>
			<div class="containerMCE">
				<textarea name="description" id="description"><?= $tournament->getDescription(); ?></textarea>
			</div>
			<br />
			<div class="label">Categor&iacute;a: </div>
			<select name="class_id" id="class_id">
			<?php 
			foreach ($tournamentCategories as $category){
			?>
				<option value="<?=$category->getClassId(); ?>" <?php echo ($SelectedCategory == $category->getClassId()) ? 'selected="selected"' : ''; ?>><?= utf8_decode($category->getDescription());?></option>
			<?php 
			}
			?>
			</select>
			<br />
			<br />
			<div class="label">Logo: </div><input type="file" name="image" id="image" />
			<?php 
			$image = $tournament->getImage();
			if (!empty($image)){
			?>
			<div class="logo">
				<img src="<?= MAIN_TOURNAMENTLOGO_NEWS_PATH . $image;?>" />
				<br />
				<input type="checkbox" name='deleteImage' id='deleteImage' value='1' /><label for="deleteImage">Eliminar Imagen</label>
			</div>
			<?
			}
			?>
			<br />
			<br />
			<p>Nota: si activa la siguiente opci&oacute;n se crear&aacute; una copia id&eacute;ntica del torneo pero de sexo femenino por lo tanto aseg&uacute;rese de estar creando el torneo masculino.</p>
			<input type="checkbox" name='createFemale' id='createFemale' value='1' /><label for="createFemale">Crear Femenino</label>
			<br />
			<br />
			<input type="checkbox" name='active' id='active' value='1' <? echo $tournament->getActive() == 1 ? 'checked = "checked"' : ''; ?> /><label for="active">Activo</label>
			<br />
			<div id="button"><input type="submit" name='sbmSubmit' id='sbmSubmit' value='Aceptar' /></div>
		</form>
		<img class="back" src='<?= ADMINISTRATOR_IMAGE_PATH . 'back.png'; ?>' id="back" width='64' height='52'/>
	</div>
</div>

<?php
	FunctionsHelper::includeBottom();
?>